package com.jet.chat.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jet.chat.exception.ChatExceptionClientReaderNotAvailable;
import com.jet.chat.exception.ChatExceptionClientWriterNotAvailable;
import com.jet.chat.exception.ChatExceptionServerHostNotAvailable;
import com.jet.chat.message.MessageReaderBufferedReader;
import com.jet.chat.message.MessageReaderObjectInputStream;
import com.jet.chat.message.MessageWriterObjectOutputStream;
import com.jet.chat.message.MessageWriterPrintWriter;
import com.jet.chat.service.Arguments;
import com.jet.chat.thread.ChatThread;


public class ChatClient {
	private Socket socket;
	private ObjectInputStream reader;
	private ObjectOutputStream writer;
	
	public ChatClient(String serverHost, int serverPort) throws ChatExceptionServerHostNotAvailable, ChatExceptionClientReaderNotAvailable, ChatExceptionClientWriterNotAvailable {
		try {
			this.socket = new Socket(serverHost, serverPort);
		} catch (UnknownHostException e) {
			throw new ChatExceptionServerHostNotAvailable("host not found");
		} catch (IOException e) {
			throw new ChatExceptionServerHostNotAvailable("ololo netnet net");
		}

		try {
			this.writer = new ObjectOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			throw new ChatExceptionClientWriterNotAvailable("ошибка создания потока вывода клиента");
		}
		
		try {
			this.reader = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			throw new ChatExceptionClientReaderNotAvailable("ошибка создания потока чтения клиента");
		}
		
	}
	
	public void execute() {
		//    /vch 2012-05-18-00.00.00
		
		
		
		new Thread(
				new ChatClientReaderThread(
						new MessageReaderObjectInputStream(this.reader),
						new MessageWriterPrintWriter(new PrintWriter(System.out)),
						new MessageWriterObjectOutputStream(this.writer)
				)
		).start();
		
		//new Thread(new ChatThread(new MessageReaderObjectInputStream(this.reader), new MessageWriterPrintWriter(new PrintWriter(System.out)), "server shutdown")).start();
		new Thread(new ChatThread(new MessageReaderBufferedReader(new BufferedReader(new InputStreamReader(System.in))), new MessageWriterObjectOutputStream(this.writer), "client shutdown")).start();
	}

	public static void main(String[] arg) {
		ChatClient chatClient;
		try {
			String hostname = "localhost";
			int port = 30030;

			Arguments arguments = new Arguments(arg);
			String hostnameArg = arguments.getValue("hostname");
			if (hostnameArg != null && !hostnameArg.equals("")) {
				hostname = hostnameArg;
			}

			chatClient = new ChatClient(hostname, port);
			chatClient.execute();
		} catch (ChatExceptionServerHostNotAvailable e) {
			System.out.println("Сервер недоступен");
			 //[ek] TODO need more user-friendly messages
		} catch (ChatExceptionClientReaderNotAvailable e) {
			System.out.println("Ошибка потока чтения клиента");
		} catch (ChatExceptionClientWriterNotAvailable e) {
			System.out.println("Ошибка потока вывода клиента");
		}
	}
}
